package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.suozhang.framework.entity.bo.PageData;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.common.util.DateTool;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.entity.bo.BallMatchBo2;
import com.yiqi.lottery.feature.football.adapter.FootballSFCAdapter;
import com.yiqi.lottery.feature.football.contract.FootballContract;
import com.yiqi.lottery.feature.football.dependencies.football.DaggerFootballComponent;
import com.yiqi.lottery.feature.football.dependencies.football.FootballPresenterModule;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 胜负彩/任选9
 */
public class FootballSFCActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, FootballContract.View {

    @Inject
    FootballContract.Presenter mPresenter;

    @BindView(R.id.view_toolbar)
    RelativeLayout view_toolbar;
    @BindView(R.id.img_back)
    ImageView img_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.img_shaixuan)
    ImageView img_shaixuan;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.img_delete)
    ImageView img_delete;
    @BindView(R.id.tv_selected)
    TextView tv_selected;
    @BindView(R.id.tv_tips)
    TextView tv_tips;
    @BindView(R.id.tv_buy)
    TextView tv_buy;

    FootballSFCAdapter mAdapter;
    @BindView(R.id.tv_isuee)
    TextView tvIsuee;
    private int mCurrentBetType;  //9表示胜负彩，10表示任选9

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_football_match_shengfucai;
    }

    @Override
    protected void initView() {
        mCurrentBetType = getIntent().getIntExtra("current_type", 0);
        tv_title.setText(mCurrentBetType == 9 ? "胜负彩" : "任选九");
        tv_tips.setText("请选择至少" + (mCurrentBetType == 9 ? 14 : 9) + "场比赛");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        getAdapter().bindToRecyclerView(recyclerView);
    }

    public void onResume() {
        super.onResume();
        getAdapter().notifyDataSetChanged();
    }

    private FootballSFCAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new FootballSFCAdapter(this);
            mAdapter.setOnCheckedNumChangeListener(new FootballSFCAdapter.OnCheckedNumChangeListener() {
                @Override
                public void onCheckedNumChange(int num) {
                    tv_selected.setText(Html.fromHtml("已选<font color=\"#D83220\">" + num + "</font>场"));
                }
            });
        }
        return mAdapter;
    }

    @Override
    protected void initInjector() {
        DaggerFootballComponent.builder().footballPresenterModule(new FootballPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {
        tv_selected.setText(Html.fromHtml("已选<font color=\"#D83220\">" + 0 + "</font>场"));
        mPresenter.getFootballSFCMatchList();
    }

    @OnClick({R.id.img_back, R.id.img_shaixuan, R.id.img_delete, R.id.tv_buy})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_shaixuan:
                if (mCurrentBetType == 0) {

                }
                //               startActivity(new Intent(this, FootballMatchFilterActivity.class));
                break;
            case R.id.img_delete:
                getAdapter().resetCheck();
                break;
            case R.id.tv_buy:
                int checkNum = getAdapter().getCheckNum();
                if (mCurrentBetType == 9 && checkNum < 14) {
                    T.showShort("请至少选择14场比赛！");
                    return;
                }
                if (mCurrentBetType == 10 && checkNum < 9) {
                    T.showShort("请至少选择9场比赛！");
                    return;
                }
                DataManager.getInstance().setTempObject(getAdapter().getMatchBo());
                Intent intent = new Intent(this, FootballSFCCalculateMoneyActivity.class);
                intent.putExtra("current_type", mCurrentBetType);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void showSFCMatchSuccess(BallMatchBo matchBo) {
        if (matchBo != null)
            tvIsuee.setText(Html.fromHtml("第"+matchBo.getIssue()+"期&nbsp;&nbsp;&nbsp;&nbsp;"+"<font color=\"#D83200\">"+ matchBo.getStopTime()+"截至投注</font>"));
        getAdapter().setMatchBo(matchBo);
        getAdapter().setNewData(matchBo.getMatch());
    }

    @Override
    public void showFootballMatchListSuccess(PageData<BallMatchBo> data) {

    }


    @Override
    public void showFootballMatchListSuccess2(PageData<BallMatchBo2> data) {

    }

    @Override
    public void showFootballMatchListError(String error) {
        T.showShort(error);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
